﻿using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using AutoMapper;

namespace AutoMapperSamples.Samples
{
    /// <summary>
    /// 嵌套映射测试类型
    /// </summary>
    [TestClass]
    public class NestedMapping
    {      

        [TestMethod]
        public void NestedMappingTest()
        {
            //初始化
            Mapper.Initialize(cfg => {
                /*******这里是重点*******/
                cfg.CreateMap<InnerSource, InnerDest>();
                cfg.CreateMap<OuterSource, OuterDest>();
            });
            var src = new OuterSource
            {
                Value = 1,
                Inner = new InnerSource { OtherValue =2}
            };
            OuterDest dest = Mapper.Map<OuterSource, OuterDest>(src);

            //验证结果
            Assert.AreEqual(1, dest.Value);
            Assert.IsNotNull(dest.Inner);
            Assert.AreEqual(2, dest.Inner.OtherValue);
        }
    }
    /// <summary>
    /// 源对象
    /// </summary>
    public class OuterSource
    {
        public int Value { get; set; }
        public InnerSource Inner { get; set; }
    }
    /// <summary>
    /// 源嵌套对象
    /// </summary>
    public class InnerSource
    {
        public int OtherValue { get; set; }
    }

    /// <summary>
    /// 目标对象
    /// </summary>
    public class OuterDest
    {
        public int Value { get; set; }
        public InnerDest Inner { get; set; }
    }
    /// <summary>
    /// 目标嵌套对象
    /// </summary>
    public class InnerDest
    {
        public int OtherValue { get; set; }
    }

}
